package com.tencent.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tencent.entity.Result;
import com.tencent.entity.User;
import com.tencent.mapper.LoginMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @BelongsProject: health2.2
 * @BelongsPackage: com.tencent.controller
 * @ClassName: RegisterController
 * @Description: TODO
 * @Version: 1.0.0
 * @CreateTime: 2022-09-03  09:28
 * @Author: Yangjx
 */
@RestController
@RequestMapping("/register")
public class RegisterController {
    @Autowired
    private LoginMapper loginMapper;

    @PostMapping
    public Result sign(@RequestBody User user){
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("username",user.getUsername());
        User selectUser = loginMapper.selectOne(qw);
        if (selectUser == null) {//查无此人
            String password = user.getPassword();
            user.setPassword(BCrypt.hashpw(password,BCrypt.gensalt()));
            //生成新用户
            int i = loginMapper.insert(user);
            if (i == 0) {
                return Result.registerQuestion("注册失败,请联系管理员(18255779342)");
            } else {
                return Result.success("注册成功,请去登录.");
            }
        }
        return Result.registerQuestion("用户名已被占用,请更换用户名.");
    }
}
